/* 展开指定节点 */
var selecteGroupNodePath = '/0';

var selectedGroupNodePathMap = new HashMap();
selectedGroupNodePathMap.put(selecteGroupNodePath, selecteGroupNodePath);

/**
 * 机构/人员选择树
 * 
 * @param {}
 *            defaultIds
 * @param {}
 *            oid
 * @param {}
 *            oname
 */
function selectGroupOnly(oid, oname, root, single, func) {
	var url = context + '/system/group/groupmapscript.jsp?type=ALL_GROUP_CHECK';

	if (root && root != '') {
		url += '&id=' + root;
	}

	var selectGrouptree = new Ext.tree.TreePanel({
				id : 'selectGrouptree',
				// singleExpand : true,
				frame : false,
				checkModel : 'single',
				// onlyLeafCheckable : true,
				border : false,
				autoScroll : true,
				animate : true,
				rootVisible : false,
				loader : new Ext.tree.TreeLoader({
							dataUrl : url,
							baseParams : {
				// active : 'purview4GUSelectForm'
							}
						}),
				root : new Ext.tree.AsyncTreeNode({
							allowChildren : true,
							expanded : true,
							text : '组根节点', // 节点名称
							draggable : false, // 是否支持拖动
							id : '0' // 节点id
						}),
				listeners : {
					'click' : function(node) {
						if (node.isExpanded()) {
							node.collapse();
						} else {
							node.expand();
						}
					},
					'load' : function(node) {
						if (node.id == '0') {
							/* 手工添加临时组节点 */
							// selectGrouptree.root.appendChild(temp);
						}

						if (Ext.get(oid)) {
							for (var j = 0; j < node.childNodes.length; j++) {
								if (hasSelectGroup(Ext.get(oid).dom.value,
										node.childNodes[j].attributes.realid)) {
									node.childNodes[j].attributes.checked = true;
									// node.childNodes[j].ui.checkbox.checked =
									// true;
								}
							}

						}
					}

				}
			});

	/*
	 * 选中节点后,自动选中其下所有子节点 selectGrouptree.on('checkchange', function(node,
	 * checked) { node.expand(); node.attributes.checked = checked;
	 * node.eachChild(function(child) { child.ui.toggleCheck(checked);
	 * child.attributes.checked = checked; child.fireEvent('checkchange', child,
	 * checked); }); }, selectGrouptree);
	 */

	selectGrouptree.on('checkchange', function(node, checked) {
				if (checked && single) {
					var selNodes = selectGrouptree.getChecked();
					Ext.each(selNodes, function(nd) {
								if (node.id != nd.id) {
									nd.ui.toggleCheck(false);
									nd.attributes.checked = false;
								}
							});
				}
			}, selectGrouptree);

	var temp = new Ext.tree.TreeNode({
				id : 'gtemp',
				realid : 'temp',
				type : 'group',
				flumelabel : '',
				iconCls : 'icon-grouptemp',
				checked : false,
				text : '临时组'
			});

	var groupnodespath = selectedGroupNodePathMap.values();

	for (var i = 0; i < groupnodespath.length; i++) {
		/* 展开指定节点 */
		selectGrouptree.expandPath(groupnodespath[i]);
	}

	var selectGrouptreewin = new Ext.Window({
				id : 'selectGrouptreewin',
				renderTo : Ext.getBody(),
				layout : 'fit',
				width : 300,
				height : 400,
				title : '组织机构树',
				resizable : false,
				plain : true,
				modal : true,

				items : [selectGrouptree],

				buttons : [{
							text : '提 交',
							handler : function() {
								handleSelectGroup(oid, oname);

								if (func && typeof func == 'function') {
									try {
										func()
									} catch (error) {
										alert('回调函数[\'' + func + '\']调用出现异常');
									}
								}
							}
						}, {
							text : '关 闭',
							handler : function() {
								selectGrouptreewin.close();
							}
						}]
			});
	selectGrouptreewin.show(this);
}

/*
 * , bbar : [{ text : "选 择", iconCls : 'icon-pselectuser', handler : function() {
 * var names = '', ids = '', types = '', selNodes = Ext
 * .getCmp('selectlanmutree').getChecked(); Ext.each(selNodes, function(node) {
 * if (names.length > 0) { names += ','; ids += ','; types += ','; } names +=
 * node.text; ids += node.attributes.realid; types += node.attributes.type; }); } }, {
 * text : "取 消", iconCls : 'icon-punchk', handler : function() { } }]
 */

function hasSelectGroup(ids, id) {
	var rnt = false;
	var idstr = ids.split(',');
	for (var i = 0; i < idstr.length; i++) {
		if (idstr[i] == id) {
			rnt = true;
			break;
		}
	}

	return rnt;
}

function handleSelectGroup(oid, oname) {
	var names = '', ids = '', types = '';
	var selNodes = Ext.getCmp('selectGrouptree').getChecked();
	Ext.each(selNodes, function(node) {
				var idpath = node.getPath('id');
				selectedGroupNodePathMap.put(idpath, idpath);

				var path = node.getPath('text');
				path = getGroupNodePath(path);

				if (names.length > 0) {
					names += ',';
					ids += ',';
					types += ',';
				}
				// names += node.text;
				names += path;
				ids += node.attributes.realid;
			});

	if (Ext.get(oid)) {
		Ext.get(oid).dom.value = ids;
	}

	if (Ext.get(oname)) {

		Ext.get(oname).dom.value = names;
	}

	Ext.getCmp('selectGrouptreewin').close();
}

function getGroupNodePath(path) {
	var pathstr = path.split('/');

	if (pathstr.length > 2) {
		var pathName = "";

		for (var i = 2; i < pathstr.length; i++) {
			pathName += pathstr[i];

			if (i < pathstr.length - 1) {
				pathName += '/';
			}
		}

		return pathName;
	}

	return '';
}